/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jcac_1_0.m;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.IOException;
import jcac_1_0.util.JsonUtil;

/**
 *
 * @author SoChishun
 */
public class ConfigModels extends BaseModels {

    public ConfigModels() {
        super("t_porg_config");
    }

    public WorkTime GetWorkTime() throws IOException {
        Object oval = super.getField("value", "name='WORKTIME'");
        if (oval == null) {
            return new WorkTime();
        }
        String sval = oval.toString();
        return JsonUtil.deserialize(WorkTime.class, sval);
    }

    public boolean SaveWorkTime(WorkTime wt) throws JsonProcessingException {
        String sname = "WORKTIME";
        String sval = "";
        sval = JsonUtil.serialize(wt);
        int n, exists = Integer.parseInt(super.getField("count(*)", String.format("`name`='%s'", sname)).toString());
        if (exists > 0) {
            n = super.executeUpdate(String.format("update %s set `value`='%s' where `name`='%s';", this.tableName, sval, sname));
        } else {
            n = super.executeUpdate(String.format("insert into %s (`name`,`value`) values ('%s','%s');", this.tableName, sval, sname));
        }
        return n > 0;
    }

}
